package org.tech4j.edi.base.executor;

import org.tech4j.edi.api.RetryExecutor;
import org.tech4j.edi.api.RetryExecutorChain;
import org.tech4j.edi.api.context.RetryContext;
import org.tech4j.edi.base.exception.EdiException;
import org.tech4j.edi.api.factory.RetryHandleFactory;
import org.tech4j.edi.api.factory.RetryHandleFactoryProxy;
import org.tech4j.edi.common.RetryHandle;

import java.util.Objects;

/**
 * @program: tech4j
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/7 21:18
 **/
public class RetryContextRetryExecutor implements RetryExecutor {

    @Override
    public void execute(RetryContext retryContext, RetryExecutorChain chain) {
        RetryHandleFactory retryHandleFactory = RetryHandleFactoryProxy.newInstance(retryContext.getHandleFactoryName());
        if (Objects.isNull(retryHandleFactory)) {
            throw new EdiException("RetryHandleFactory is null");
        }
        RetryHandle retryHandle = retryHandleFactory.getRetryHandle(retryContext.getRetryBizConfig().getBizType());
        retryContext.setRetryHandle(retryHandle);
        chain.execute(retryContext);
    }

    @Override
    public int getOrder() {
        return EdiExecutorsOrder.RETRY_CONTEXT.getOrder();
    }
}
